﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModels.Common;
using GenericTools;
using GenericTools.Models;

namespace ReportingManagement.Components.AutoReminder
{
    public partial class SetReportMesssageTemplates : Form
    {
        #region Constants
        private const int ENABLED_COLUMN_INDEX = 0;
        private const int REPORT_NAME_COLUMN_INDEX = 1;
        private const int MESSAGE_TEMPLATE_COLUMN_INDEX = 2;
        #endregion

        public SetReportMesssageTemplates()
        {
            InitializeComponent();

            dgvCboReportName.ValueMember = "RepID";
            dgvCboReportName.DisplayMember = "ReportName";
            dgvCboReportName.DataSource = _Reports;
            
            foreach (DataRow r in _Reports.Rows)
            {
                int rowIndex = dgvReportMessageTemplates.Rows.Add();
                DataGridViewRow row = dgvReportMessageTemplates.Rows[rowIndex];
                row.Cells[REPORT_NAME_COLUMN_INDEX].Value = int.Parse(r["RepID"].ToString());
            }

            RestoreTemplateBasedOnDBRecords();
        }

        private void RestoreTemplateBasedOnDBRecords()
        {
            DataTable dataTable = Utilities.GetAutoReminderReportMessageTemplates();
            btnReset.Enabled = (dataTable.Rows.Count == _Reports.Rows.Count);

            foreach (DataGridViewRow row in dgvReportMessageTemplates.Rows)
            {
                int cellReportId = int.Parse(row.Cells[REPORT_NAME_COLUMN_INDEX].Value.ToString());
                bool rowRestored = false;
                foreach (DataRow r in dataTable.Rows)
                {
                    int reportId = int.Parse(r["ReportId"].ToString());
                    if (cellReportId == reportId)
                    {
                        bool enabled = bool.Parse(r["EnableAutoReminder"].ToString());
                        string messageTemplate = r["MessageTemplate"].ToString();
                        row.Cells[ENABLED_COLUMN_INDEX].Value = enabled;
                        row.Cells[MESSAGE_TEMPLATE_COLUMN_INDEX].Value = messageTemplate;

                        //dataTable.Rows.Remove(r);
                        rowRestored = true;
                        break;
                    }
                }

                if (!rowRestored)   // set the default values;
                {
                    row.Cells[ENABLED_COLUMN_INDEX].Value = false;
                    row.Cells[MESSAGE_TEMPLATE_COLUMN_INDEX].Value = string.Empty;
                }
            }
            CreateEnabledReportsAndTemplates();
        }

        private static DataTable _Reports;
        public static DataTable Reports
        {
            get
            {
                return _Reports;
            }
        }

        static SetReportMesssageTemplates()
        {
            _Reports = Utilities.GetAllExpectedReports();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            RestoreTemplateBasedOnDBRecords();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            List<int> reportIds = new List<int>();
            List<bool> enabled = new List<bool>();
            List<string> messageTemplates = new List<string>();

            foreach (DataGridViewRow r in dgvReportMessageTemplates.Rows)
            {
                int id = int.Parse(r.Cells[REPORT_NAME_COLUMN_INDEX].Value.ToString());
                bool enable = bool.Parse(r.Cells[ENABLED_COLUMN_INDEX].Value.ToString());
                string template = r.Cells[MESSAGE_TEMPLATE_COLUMN_INDEX].Value.ToString();

                reportIds.Add(id);
                enabled.Add(enable);
                messageTemplates.Add(template);
            }

            try
            {
                Utilities.SaveAutoReminderReportMessageTemplates(enabled, reportIds, messageTemplates);
                string saveDate = DateTime.Now.ToString("s");
                Utilities.SetSetting(AutoReminderSettings.AUTO_REMINDER_SETTING_SAVE_DATE, saveDate);
                
                CreateEnabledReportsAndTemplates();
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, String.Format("Error while trying to save: {0}", ex.Message),
                    AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void CreateEnabledReportsAndTemplates()
        {
            _enabledReportsAndTemplates = new List<string>();
            foreach (DataGridViewRow r in dgvReportMessageTemplates.Rows)
            {                
                object enabledObject = r.Cells[ENABLED_COLUMN_INDEX].Value;
                if (enabledObject != null)
                {
                    bool enable = bool.Parse(enabledObject.ToString());

                    if (enable)
                    {
                        int id = int.Parse(r.Cells[REPORT_NAME_COLUMN_INDEX].Value.ToString());
                        string template = r.Cells[MESSAGE_TEMPLATE_COLUMN_INDEX].Value.ToString();
                        string reportName = string.Empty;

                        foreach (DataRow row in _Reports.Rows)
                        {
                            int reportId = int.Parse(row["RepID"].ToString());
                            if (reportId == id)
                            {
                                reportName = row["ReportName"].ToString();
                                break;
                            }
                        }
                        string displayText = String.Format("{0} - {1}", reportName, template);
                        _enabledReportsAndTemplates.Add(displayText);
                    }
                }
            }
        }

        private List<string> _enabledReportsAndTemplates;
        public List<string> EnabledReportsAndTemplates
        {
            get
            {
                return _enabledReportsAndTemplates;
            }
        }
    }
}
